﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using qiupeng.Public;
using System.IO;

public partial class JiGouManage_JgMain : System.Web.UI.Page
{
    Db List = new Db();
    public static string showtitle;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Session["username"] == null)
        {
            this.Response.Write("<script language=javascript>alert('登陆超时！');window.parent.location = '../main_d.aspx'</script>");
            return;

        }





        if (!Page.IsPostBack)
        {
            BindAttribute();

            string SQL_GetList = "select * from qp_hr_JiGou where id='" + List.GetFormatStr(Request.QueryString["id"]) + "'";
            OleDbDataReader NewReader = List.GetList(SQL_GetList);
            if (NewReader.Read())
            {
                showtitle = "部门信息";
                Name.Text = NewReader["JgName"].ToString();
                BianZhiS.Text = NewReader["BianZhiS"].ToString();
                remark.Text = List.TbToLb(NewReader["remark"].ToString());
                List.InsertLog("查看部门[" + Name.Text + "]", "部门管理");
                Button3.Visible = true;
                Button1.Visible = true;
                Button2.Visible = true;

                string SQL_GetList1 = "select * from qp_hr_JiGou where id='" + NewReader["ParentNodesID"].ToString() + "'";
                OleDbDataReader NewReader1 = List.GetList(SQL_GetList1);
                if (NewReader1.Read())
                {
                    ParentNodesID.Text = NewReader1["jgname"].ToString();
                }
                NewReader1.Close();

            }
            else
            {
                showtitle = "<font color=red>无部门信息，请点击左边树型菜单或点击［<a href=JgMain_add.aspx>增加</a>］</font>";
                Button3.Visible = false;
                Button1.Visible = false;
                Button2.Visible = false;

            }
            NewReader.Close();

            Bindquanxian();

        }






        string SQL_GetList_xs = "select * from  qp_hr_JiGou   where  ParentNodesID='" + List.GetFormatStr(Request.QueryString["id"]) + "' ";
        OleDbDataReader NewReaderxs = List.GetList(SQL_GetList_xs);
        while (NewReaderxs.Read())
        {
            XiaJiBm.Text += "" + NewReaderxs["JgName"].ToString() + "，";

        }
        NewReaderxs.Close();




        string SQL_GetList_gw = "select * from  qp_hr_JiGou_GW   where  JgNameId='" + List.GetFormatStr(Request.QueryString["id"]) + "' ";
        OleDbDataReader NewReadergw = List.GetList(SQL_GetList_gw);
        while (NewReadergw.Read())
        {
            GangWeiBZ.Text += "" + NewReadergw["GangWei"].ToString() + "(" + NewReadergw["BianZhiS"].ToString() + "人)，";
        }
        NewReadergw.Close();




    }
    public void BindAttribute()
    {
        Button2.Attributes["onclick"] = "javascript:return showwait();";
        Button1.Attributes["onclick"] = "javascript:return deltree();";

        Button3.Attributes["onclick"] = "javascript:return showwait();";
        Button4.Attributes["onclick"] = "javascript:return showwait();";

    }
    public void Bindquanxian()
    {
        List.QuanXianVis(Button3, "aaaa1d", Session["perstr"].ToString());
        List.QuanXianVis(Button1, "aaaa1e", Session["perstr"].ToString());
        List.QuanXianVis(Button2, "aaaa1b", Session["perstr"].ToString());
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("JgMain_add.aspx?id=" + List.GetFormatStr(Request.QueryString["id"]) + "");
    }


    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("JgMain_update.aspx?id=" + List.GetFormatStr(Request.QueryString["id"]) + "");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //string SQL_GetList_check = "select * from qp_hr_Staff  where   UnitId='" + List.GetFormatStr(Request.QueryString["id"]) + "'";

        //OleDbDataReader NewReader = List.GetList(SQL_GetList_check);
        //if (NewReader.Read())
        //{
        //    this.Response.Write("<script language=javascript>alert('删除失败，其中存在正在使用的部门，请在人事资料中查看！');</script>");
        //    return;

        //}

        //NewReader.Close();

        DelNode(Request.QueryString["id"], Request.QueryString["id"]);
        this.Response.Write("<script language=javascript>alert('删除成功！'); window.parent.lform.location = '../Agenciestable.aspx'</script>");

    }

    private void DelNode(string IDStr, string PID)
    {
        string SqlStr = "  Delete from qp_hr_JiGou  where id='" + IDStr + "'";
        List.ExeSql(SqlStr);//删除最根节点

        string SQL_GetList = "select * from qp_hr_JiGou where (ParentNodesID=" + IDStr.ToString() + ")  order by id asc";
        OleDbDataReader MyReader = List.GetList(SQL_GetList);
        while (MyReader.Read())
        {
            string SqlStr1 = "  Delete from qp_hr_JiGou  where id='" + MyReader["ID"].ToString() + "'";
            List.ExeSql(SqlStr1);
            string SQL_GetList1 = "select * from qp_hr_JiGou where ParentNodesID=" + MyReader["id"] + " ";
            OleDbDataReader MyReader1 = List.GetList(SQL_GetList1);
            if (MyReader1.Read())
            {

                string IDStr1 = MyReader["ID"].ToString();
                string PID1 = MyReader["ParentNodesID"].ToString();
                DelNode(IDStr1, PID1);

            }
            MyReader1.Close();

        }
        MyReader.Close();

    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("JgMain_BZ.aspx?id=" + List.GetFormatStr(Request.QueryString["id"]) + "");
    }
}
